package cn.cxy.myapplication;

import android.content.Context;
import android.util.Log;

/**
 * 全局异常捕获类
 */
public class UnCatchExceptionHandler implements Thread.UncaughtExceptionHandler {


    private Context context;//留作备用

    private Thread.UncaughtExceptionHandler defaultExceptionHandler;//系统的默认异常处理类

    private static UnCatchExceptionHandler instance = new UnCatchExceptionHandler();//用户自定义的异常处理类



    private UnCatchExceptionHandler() {
    }

    public static UnCatchExceptionHandler getInstance(){
        return instance;
    }

    public void  init(Context context){
        this.context=context.getApplicationContext();
        defaultExceptionHandler =Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Log.e("dddd",e.getMessage());

        //如果系统提供了异常处理类，则交给系统去处理
        if (defaultExceptionHandler != null) {
            defaultExceptionHandler.uncaughtException(t,e);
        }else {

            //否则我们自己处理，自己处理通常是让app退出
//            Process.killProcess(Process.myPid());
        }
    }
}